package ru.spb.nicetu.schoolproject.gui;

import ru.spb.nicetu.schoolproject.data.Subject;
import ru.spb.nicetu.schoolproject.data.Teacher;

import javax.swing.*;
import java.awt.*;

import java.text.NumberFormat;

/**
 * Created by dchernyshov on 07.04.15.
 */
public class TeacherCard extends CardTemplate<Teacher, Subject> {
    private JFormattedTextField cabinetField;

    public TeacherCard() {
        super(Subject.class, "Карточка учителя", false, new Teacher());
        Box vbox = createVerticalBox();
        add(vbox, BorderLayout.CENTER);
        setSize(300, 150);
    }

    public TeacherCard(Teacher teacher) {
        super(Subject.class, "Карточка учителя", true, teacher);
        Box vbox = createVerticalBox();
        add(vbox, BorderLayout.CENTER);
        lessonNumField.setText(teacher.getTeacherName().trim());
        comboBox.setSelectedItem(teacher);
        cabinetField.setValue(teacher.getCabinet());
    }

    public void inputValue(Teacher t) {
        t.setTeacherName(lessonNumField.getText().trim());
        Number cabinet = (Number) cabinetField.getValue();
        t.setCabinet(cabinet.intValue());
        t.setSubject((Subject) comboBox.getSelectedItem());
    }

    @Override
    public boolean checkCard() {
        if (lessonNumField.getText().isEmpty()) {
            errorDialog("Введите ФИО!");
            return false;
        }
        if (cabinetField.getText().isEmpty()) {
            errorDialog("Введите номер кабинета");
            return false;
        }
        return true;
    }

    public Box createThirdHorizontalBox() {
        Box box = Box.createHorizontalBox();
        JLabel cabinetLabel = new JLabel("Кабинет:");
        box.add(cabinetLabel);
        box.add(Box.createHorizontalStrut(10));
        cabinetField = new JFormattedTextField(NumberFormat.getIntegerInstance());
        cabinetField.setColumns(4);
        cabinetField.setMaximumSize(cabinetField.getPreferredSize());
        box.add(cabinetField);
        box.add(Box.createHorizontalGlue());
        return box;
    }

    public Box createVerticalBox() {
        Box box = Box.createVerticalBox();
        Box hbox1 = createFirstHorizontalBox("ФИО:");
        box.add(hbox1);
        box.add(Box.createVerticalStrut(10));
        Box hbox2 = createSecondHorizontalBox("Дисциплины:");
        box.add(hbox2);
        box.add(Box.createHorizontalStrut(10));
        Box hbox3 = createThirdHorizontalBox();
        box.add(hbox3);
        box.add(Box.createHorizontalStrut(10));
        box.add(createLastHorizontalBox());
        box.add(Box.createVerticalGlue());
        return box;
    }
}
